
//
//  ECUIVC_Home.m
//  Edu_iOS2018
//
//  Created by yanyan on 2018/1/4.
//  Copyright © 2018年 yanyan. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "ECUIVC_Home.h"
#import "ECUICVCell_HomeScroll.h"
#import "ECUITVCell_HomeAllCategory.h"
#import "ECUICVCell_Home.h"
#import "ECUICVHeader_Home.h"
#import "UINavigationBar+Awesome.h"
#import "CYUICVHeader_base.h"

#import "ECUIVC_Login.h"
#import "UIStory_CLogin.h"
#import "CYAnimation.h"
#import "ECUIVC_CourseDetail.h"
#import "CYUIView_Tabbar.h"
#import "ECModelData.h"
#import "ECUICVCell_HomeCategory.h"
#import "ECData_Format.h"
#import "ECUIVC_CourseList.h"
#define SELECTEDBUTTONFONT_NORMALSIZE  13 * kPercentage
#define SELECTEDBUTTONFONT_SELECTEDSIZE  15 * kPercentage
#import "UICVFloatingHeaderFlowLayout.h"
#import "ECUIView_SelectedCategory.h"
#import "ECUITVCell_Container.h"
#import "ECUIVC_HomeCourse.h"
#import "searchPageViewController.h"
#import "ECScanViewController.h"
#import "StyleDIY.h"

/******************************/

extern  CGFloat CYTabBarViewTabOffsetInvalid;
@interface ECUIVC_Home ()<UITableViewDelegate,UITableViewDataSource,DZNEmptyDataSetSource,DZNEmptyDataSetDelegate,SDCycleScrollViewDelegate,ECContainerCellDelegate>
{
    ECUIVC_CourseDetail *uivc_detail;
    NSURLSessionDataTask *task;
}
@property(nonatomic,strong)UIColor *showColor;
@property(nonatomic,strong)ECUILinkTableView *scrollView;//最底层的 View
@property(nonatomic,strong)ECUICVHeader_Home *sectionHeaderView;// 二级分类置顶的 View
@property(nonatomic,strong)SDCycleScrollView *adScrollView;//广告轮播图
@property(nonatomic,strong)UIButton *searchBtn;
@property(nonatomic,strong)NSMutableArray *dataList;//首页数据，以分组的形式
@property(nonatomic,strong)NSMutableArray *categorysArray;//分类数据-分类页的数据
@property(nonatomic,strong)NSMutableArray *allCategorysArray;//分类数据- 带有全部的

@property(nonatomic,strong)NSMutableArray *uivcsArr;
@property(nonatomic,strong)PrimaryCategoryModel *interestCategory;//关注的二级分类
@property(nonatomic,strong)PrimaryCategoryModel *selectCategory;//选中的三级分类
@property(nonatomic,strong)ECUIView_SelectedCategory *uiview_category;
@property(nonatomic, assign)BOOL headerFloating;
@property(nonatomic,strong)ECUITVCell_Container *containerCell;
@property(nonatomic,assign)BOOL canScroll; // 是否可以滚动
@end

@implementation ECUIVC_Home

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.edgesForExtendedLayout = UIRectEdgeTop;//让页面显示到顶端
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@""style:UIBarButtonItemStylePlain target:self action:nil];
    [self.view addSubview:self.scrollView];
    self.canScroll = YES;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeScrollStatus) name:@"leaveTop" object:nil];
    [self requestHomeData];
    
}
- (NSMutableArray *)dataList
{
    if (!_dataList) {
        _dataList = [NSMutableArray array];
    }
    return _dataList;
}

//  通知
- (void)changeScrollStatus {
    self.canScroll = YES;
    self.scrollView.scrollEnabled = YES;
    self.containerCell.objectCanScroll = NO;
    [self setSubVcCanScroll:NO];
    
}
-(void)setSubVcCanScroll:(BOOL)flag{
    for (id obj in self.uivcsArr) {
        if ([obj isKindOfClass:[ECUIVC_HomeCourse class]]) {
            ECUIVC_HomeCourse *uivc = (ECUIVC_HomeCourse *)obj;
            uivc.vcCanScroll = flag;
            if (!flag) {
                [uivc.collectionView setContentOffset:CGPointZero animated:NO];
                
            }
        }
    }
}
#pragma mark --页面 View --

- (SDCycleScrollView *)adScrollView
{
    if (!_adScrollView) {
        _adScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, kScreenWidth, 200) delegate:nil placeholderImage:nil];
        _adScrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        _adScrollView.placeholderImage = [UIImage imageNamed:@"placeholder"];
        _adScrollView.pageControlStyle = SDCycleScrollViewPageContolStyleAnimated;
        _adScrollView.delegate = self;
        _adScrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
        _adScrollView.pageControlDotSize = CGSizeMake(11, 2);
        _adScrollView.pageDotImage = [UIImage imageNamed:@"pageviewDot"];
        _adScrollView.currentPageDotImage = [UIImage imageNamed:@"pageviewDot_current"];
    }
    return _adScrollView;
}


- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    id selectedItem = [[[self.dataList objectAtIndex:0] objectAtIndex:0] objectAtIndex:index];
    switch ([[selectedItem valueForKey:@"type"] integerValue]) {
        case 0:
            // 不跳转
            break;
        case 1:
        {
            // 跳转 url
            NSURL *url = [NSURL URLWithString:[selectedItem valueForKey:@"url"]];
            
            [[UIApplication sharedApplication] openURL:url options:nil completionHandler:^(BOOL success) {
                
            }];
        }
            break;
        case 2:
        {
            // 跳转课程
            NSString *courseId = [selectedItem valueForKey:@"courseId"];
            CourseModel *mo = [[CourseModel alloc]init];
            mo.modelId = courseId;
            ECUIVC_CourseDetail *uivc_detail = [[ECUIVC_CourseDetail alloc]init];
            uivc_detail.hidesBottomBarWhenPushed = YES;
            uivc_detail.object = mo;
            [self.navigationController pushViewController:uivc_detail animated:YES];
        }
            break;
        default:
            break;
    }
    DLog(@"--------------点击轮播图 %@",selectedItem);
}
- (void)reloadAdScrollViewData:(id)adData
{
    NSMutableArray *images = [NSMutableArray array];
    for(id obj in adData){
        [images addObject:[NSString stringWithFormat:@"%@",[obj valueForKey:@"img"]]];
        
    }
    self.adScrollView.imageURLStringsGroup = images;
}


-(NSMutableArray *)uivcsArr
{
    if (!_uivcsArr) {
        _uivcsArr = [NSMutableArray array];
    }
    return _uivcsArr;
}

- (UIView *)sectionHeaderView
{
    if (! _sectionHeaderView) {
        _sectionHeaderView = [[ECUICVHeader_Home alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 50)];
    }
    //  展示分类
    WeakSelf(weakSelf);
    _sectionHeaderView.block_selected = ^(id obj) {
        [weakSelf action_showCategory:obj];
    };
    // 显示列表
    _sectionHeaderView.block_objectAtIndex = ^(id obj, NSUInteger idx) {
        [weakSelf showCourseListWithCategory:obj atIndex:idx];
    };
    return _sectionHeaderView;
}

-(void)showCourseListWithCategory:(id)obj atIndex:(NSInteger)index{
    //
    PrimaryCategoryModel *mo = obj;
    self.selectCategory = mo;
    WeakSelf(weakSelf);
    dispatch_async(dispatch_get_main_queue(), ^{
        weakSelf.containerCell.isSelectIndex = YES;
        [weakSelf.containerCell.scrollView setContentOffset:CGPointMake(kScreenWidth * index, 0) animated:YES];
        ECUIVC_HomeCourse *uivc = [self viewControllerAtIndex:index];
        uivc.model = mo;
    });
    
    
}
#pragma mark - 点击一级分类 进入课程列表 -
- (void)showListControllerWithCategory:(PrimaryCategoryModel *)mo{
    DLog(@"----%@",mo.title);
    if (!mo.modelId || mo.modelId.length <= 0) {
        self.tabBarController.selectedIndex = 1;
        return;
    }
    ECUIVC_CourseList *uivc_list = [[ECUIVC_CourseList alloc]init];
    uivc_list.object = mo;
    uivc_list.title = @"课程列表";//mo.title;
    uivc_list.categorysData = self.allCategorysArray;
    uivc_list.selectCategory = mo;
    
    [self.navigationController pushViewController:uivc_list animated:YES];
}

- (void)addBarButtons
{
    [super addBarButtons];
    _showColor = [[UIColor whiteColor] colorWithAlphaComponent:0];
    [self.navigationController.navigationBar lt_setBackgroundColor:[[UIColor whiteColor] colorWithAlphaComponent:0]];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"scanTag"] style:UIBarButtonItemStylePlain target:self action:@selector(action_qrCode:)];
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@""style:UIBarButtonItemStylePlain target:self action:nil];
    self.navigationItem.titleView = self.searchBtn;
}
- (UIButton *)searchBtn
{
    if (!_searchBtn) {
        _searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_searchBtn setBackgroundImage:[UIImage imageNamed:@"home_title_search"] forState:UIControlStateNormal];
        _searchBtn.frame = CGRectMake(0, 6, 294, 32);
        _searchBtn.centerX = kScreenWidth/2-15;
        _searchBtn.adjustsImageWhenHighlighted = NO;
        [_searchBtn addTarget:self action:@selector(action_showSearchVC:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _searchBtn;
}
- (void)action_showSearchVC:(id)sender
{
    searchPageViewController *vc_search = [[searchPageViewController alloc]init];
    vc_search.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc_search animated:YES];
}
-(UIScrollView *)scrollView
{
    if (!_scrollView) {
        WeakSelf(weakSelf);
        _scrollView = [[ECUILinkTableView alloc]initWithFrame:CGRectMake(0, 0 , kScreenWidth, kScreenHeight - kTabbarHeight) style:UITableViewStylePlain];
        _scrollView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.delegate = self;
        _scrollView.dataSource = self;
        _scrollView.emptyDataSetSource = self;
        _scrollView.emptyDataSetDelegate = self;
        [_scrollView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
        [_scrollView registerClass:[ECUITVCell_HomeAllCategory class] forCellReuseIdentifier:@"categorycell"];
        [_scrollView registerClass:[ECUITVCell_Container class] forCellReuseIdentifier:@"containerCell"];
        _scrollView.tableFooterView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 3)];
        _scrollView.mj_header = [MJRefreshStateHeader headerWithRefreshingBlock:^{
            [weakSelf refreshHomeVC];
        }];
        
    }
    return _scrollView;
}
// 刷新 首页首页数据
- (void)refreshHomeVC{
    [self requestHomeData];
}



- (void)action_showCategory:(id)sender{
    if (!_uiview_category) {
        _uiview_category = [[ECUIView_SelectedCategory alloc]initWithFrame:CGRectMake(0, kScreenHeight, kScreenWidth, kScreenHeight)];
    }
    _uiview_category.selectedCategory = self.interestCategory;
    _uiview_category.frame = CGRectMake(0, kScreenHeight, kScreenWidth, kScreenHeight);
    WeakSelf(weakSelf);
    
    _uiview_category.categorysData = self.categorysArray;
    _uiview_category.block_selectedAtIndex = ^(id obj, NSIndexPath *indexPath) {
        [weakSelf.uiview_category removeFromSuperview];
        [weakSelf selectInterestCategoryModel:obj];
        
    };
    [[[UIApplication sharedApplication]keyWindow] addSubview:_uiview_category];
    [UIView animateWithDuration:0.25 animations:^{
        _uiview_category.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
    }];
}

#pragma mark - 选中某个 二级 分类
-(void)selectInterestCategoryModel:(PrimaryCategoryModel *)mo{
    
    self.interestCategory = mo;
    self.sectionHeaderView.interestCategory = mo;
    [[NSUserDefaults standardUserDefaults] setObject:mo.modelId forKey:kSelectedCategoryIdOfHome];
    [[NSUserDefaults standardUserDefaults]synchronize];
    [self reloadInterestCategoryModel:mo atIndex:0];
    [self showCourseListWithCategory:[mo.subCategoryArray firstObject]];
    WeakSelf(weakSelf);
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.6f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [weakSelf.containerCell.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
        
        weakSelf.sectionHeaderView.interestCategoryView.tabbarOffsetX = CYTabBarViewTabOffsetInvalid;
        
        [weakSelf.sectionHeaderView.interestCategoryView setTabIndex:0 animated:YES];
        [weakSelf.sectionHeaderView.interestCategoryView reloadData];
        
        
    });
}
#pragma mark  - 展示某个分类 下的课程列表-
-(void)showCourseListWithCategory:(id)obj{
    PrimaryCategoryModel *mo = obj;
    self.selectCategory = mo;
    ECUIVC_HomeCourse *uivc = [self viewControllerAtIndex:self.sectionHeaderView.interestCategoryView.selectedIndex];
    uivc.model = mo;
    
}
- (void)requestHomeData{
    WeakSelf(weakSelf);
    // 当正在请求，就不再重新发送请求
    if (task && task.state == NSURLSessionTaskStateRunning) {
        return;
    }
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    NSDictionary *params = nil;
    if ([[NSUserDefaults standardUserDefaults]objectForKey:kSelectedCategoryIdOfHome]) {
        params = @{@"categoryId":[[NSUserDefaults standardUserDefaults]objectForKey:kSelectedCategoryIdOfHome]};
    }
    task = [HWHttpTool get:URL_AllOfHome params:params success:^(id json) {
        [_dataList removeAllObjects];
        id data = [json valueForKey:@"data"];
        
        NSMutableArray *scrollData = [NSMutableArray array];
        NSMutableArray *recommendData = [NSMutableArray array];
        //轮播图
        if ([data valueForKey:@"carousel"]) {
            if ([[data valueForKey:@"carousel"] count]) {
                if ([[[data valueForKey:@"carousel"] objectAtIndex:0] isKindOfClass:[NSString class]]) {
                    
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [MBProgressHUD hideHUD];
                        [weakSelf.scrollView.mj_header endRefreshing];
                        [MBProgressHUD showErrorMessage:@"请求数据格式有误"];
                        [self.scrollView reloadData];
                    });
                    return ;
                }
                [scrollData addObjectsFromArray:[data valueForKey:@"carousel"]];
                
            }
            
        }
        // 首页的几个分类
        NSArray *rootCategorys = [ECData_Format arrayForHomeMenuWithData:[data valueForKey:@"rootcategories"]];
        [weakSelf.dataList addObject:@[scrollData,rootCategorys]];
        //首页默认选中的分类
        PrimaryCategoryModel *mo = [[PrimaryCategoryModel alloc]initWithObject:[data objectForKey:@"category"]];
        [self reloadInterestCategoryModel:mo atIndex:0];
        
        if ([data valueForKey:@"courses"]) {
            [self viewControllerAtIndex:0].homeData = @{@"courses":[data valueForKey:@"courses"],@"total":[data valueForKey:@"total"],@"object":mo};
        }
        [_dataList addObject:recommendData];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [MBProgressHUD hideHUD];
            [weakSelf.scrollView.mj_header endRefreshing];
            
            
            [self reloadAdScrollViewData:scrollData];
            [self reloadCategorysWithData:[data valueForKey:@"categories"]];
            [self reloadAllCategorysWithData:[data valueForKey:@"categories"]];
            [self.scrollView reloadData];
            [self.containerCell.scrollView setContentOffset:CGPointMake(0, 0)];
            
        });
        
        
        
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUD];
        //[MBProgressHUD showErrorMessage:error.description];
        /* add by lizhen  -----2018年03月26日09:33:17 */
        [MBProgressHUD showErrorMessage:@"网络连接失败"];
    }];
    
}
// 保存 所有分类
-(void) reloadCategorysWithData:(id)categorys{
    if (!_categorysArray) {
        _categorysArray = [NSMutableArray array];
    } else {
        [_categorysArray removeAllObjects];
    }
    [_categorysArray addObjectsFromArray:[ECData_Format arrayForHomeAllCategorysWithData:categorys]];
}
// 保存 所有分类 - 带有全部
-(void) reloadAllCategorysWithData:(id)categorys{
    if (!_allCategorysArray) {
        _allCategorysArray = [NSMutableArray array];
    } else {
        [_allCategorysArray removeAllObjects];
    }
    [_allCategorysArray addObjectsFromArray:[ECData_Format arrayForCategoryWithAll:categorys]];
}
#pragma mark - 重置 感兴趣的分类的数量 -
-(void)reloadInterestCategoryModel:(PrimaryCategoryModel *)mo atIndex:(NSInteger)index{
    
    self.interestCategory = mo;
    self.sectionHeaderView.interestCategory = mo;
    if (mo.subCategoryArray.count > self.uivcsArr.count) {
        [self.uivcsArr addObjectsFromArray:[mo.subCategoryArray subarrayWithRange:NSMakeRange(self.uivcsArr.count, mo.subCategoryArray.count - self.uivcsArr.count)]];
    } else if (mo.subCategoryArray.count < self.uivcsArr.count){
        [self.uivcsArr removeObjectsInRange:NSMakeRange(mo.subCategoryArray.count, self.uivcsArr.count-mo.subCategoryArray.count)];
        
    }
    
    self.containerCell.scrollView.contentSize = CGSizeMake(kScreenWidth * mo.subCategoryArray.count , self.containerCell.frame.size.height);
    WeakSelf(weakSelf);
    
    dispatch_async(dispatch_get_main_queue(), ^{
        if (weakSelf.sectionHeaderView) {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.45f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                weakSelf.sectionHeaderView.interestCategoryView.tabbarOffsetX = CYTabBarViewTabOffsetInvalid;
                
                [weakSelf.sectionHeaderView.interestCategoryView setTabIndex:0 animated:YES];
                [weakSelf.sectionHeaderView.interestCategoryView reloadData];
                
            });
        }
        
    });
    
    
}
#pragma mark - cell 的代理 -
- (void)mmtdOptionalScrollViewDidScroll:(UIScrollView *)scrollView {
    self.scrollView.scrollEnabled = NO;
}

- (void)mmtdOptionalScrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSUInteger page = scrollView.contentOffset.x/ kScreenWidth;
    DLog(@"滑动-----------%ld",page);
    [self.sectionHeaderView.interestCategoryView setTabIndex:page animated:YES];
    ECUIVC_HomeCourse *uivc = [self viewControllerAtIndex:page];
    uivc.model = [self.interestCategory.subCategoryArray objectAtIndex:page];
    
    self.scrollView.scrollEnabled = YES;
}

    //获取对应的下标 分类下的 课程列表
-(ECUIVC_HomeCourse *)viewControllerAtIndex:(NSInteger)index{
    ECUIVC_HomeCourse *uivc;
    if ([[self.uivcsArr objectAtIndex:index] isKindOfClass:[UIViewController class]]) {
        uivc = (ECUIVC_HomeCourse *)[self.uivcsArr objectAtIndex:index];
    } else {
        uivc = [[ECUIVC_HomeCourse alloc]init];
        [self.uivcsArr replaceObjectAtIndex:index withObject:uivc];
    }
    uivc.view.frame = CGRectMake(kScreenWidth * index, 0, kScreenWidth, self.containerCell.frame.size.height);
    [self.containerCell.scrollView addSubview:uivc.view];
    WeakSelf(weakSelf);
    uivc.block_selected = ^(id obj) {
        [weakSelf showCourseDetailVCWithObject:obj];
    };
    return (ECUIVC_HomeCourse *)uivc;
    
    
}
- (void)showCourseDetailVCWithObject:(id)obj
{
    //    ECUIVC_CourseDetail *
    uivc_detail = [[ECUIVC_CourseDetail alloc]init];
    uivc_detail.hidesBottomBarWhenPushed = YES;
    uivc_detail.object = obj;
    [self.navigationController pushViewController:uivc_detail animated:YES];
    
}
- (void)action_qrCode:(id)sender{
    DLog(@"二维码");
    [self openScanVCWithStyle:[StyleDIY InnerStyle]];
}

- (void)openScanVCWithStyle:(LBXScanViewStyle*)style{
    
    //style.notRecoginitonArea = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.6];
    
    ECScanViewController *vc = [ECScanViewController new];
    vc.hidesBottomBarWhenPushed = YES;
    vc.style = style;
    vc.libraryType = SLT_Native;
    vc.isOpenInterestRect = YES;
    vc.cameraInvokeMsg = @"相机启动中...";
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark -无边框，内嵌4个角
- (void)InnerStyle{
    [self openScanVCWithStyle:[StyleDIY qqStyle]];
}

#pragma mark - TableView Delegate -
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataList.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.dataList.count > 0) {
        if (section == 0) {
            return 2;
        }
        return 1;
    }
    return 0;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return nil;
    }
    return  self.sectionHeaderView;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 0;
    }
    return 50;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITVCell_base *cell;
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
            
            [cell addSubview:self.adScrollView];
        } else {
            cell = [tableView dequeueReusableCellWithIdentifier:@"categorycell" forIndexPath:indexPath];
            cell.object = [[self.dataList objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
            WeakSelf(weakSelf);
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            ((ECUITVCell_HomeAllCategory *)cell).block_selected = ^(id obj) {
                [weakSelf  showListControllerWithCategory:[obj valueForKey:@"object"]];
                
            };
        }
    } else {
        cell = [tableView dequeueReusableCellWithIdentifier:@"containerCell" forIndexPath:indexPath];
        ECUITVCell_Container *containerCell = (ECUITVCell_Container *)cell;
        self.containerCell = containerCell;
        self.containerCell.scrollView.contentSize = CGSizeMake(kScreenWidth * self.interestCategory.subCategoryArray.count , self.containerCell.frame.size.height);
        
        [self.containerCell.scrollView addSubview:[self viewControllerAtIndex:self.sectionHeaderView.interestCategoryView.selectedIndex].view];
        self.containerCell.delegate = self;
    }
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            return 200;
        }
        return 80;
    } else {
        return kScreenHeight - kNavHeight - kTabbarHeight;
        
    }
}


#pragma mark - 空数据 -

- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView{
    return [UIImage imageNamed:@"Mine_emptyImage_MineLearning"];
}

- (NSAttributedString *)buttonTitleForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state
{
    NSString *text = @"首页请求失败，【重新请求】";
    NSDictionary *attributes = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:16.0f],
                                 NSForegroundColorAttributeName: [UIColor colorWithHexString:@"#b3b3b3"]};
    return [[NSAttributedString alloc] initWithString:text attributes:attributes];
}
- (void)emptyDataSet:(UIScrollView *)scrollView didTapButton:(UIButton *)button
{
    [self requestHomeData];
}

#pragma mark - ScrollView Delegate -
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (self.dataList.count <= 1) {
        return;
    }
    if ([scrollView isEqual:self.scrollView]) {
        UIColor *color = ECTHEMESELECTEDCOLOR;
        CGFloat offsetY = scrollView.contentOffset.y;
        CGFloat bottomCellOffset = floorf([self.scrollView rectForSection:1].origin.y - kNavHeight);
        
        //当 offsety >= bottomCellOffset 时表示 titleView 到达顶端，此时 navgation 颜色和 title 颜色变成主题色
        if (offsetY > bottomCellOffset) {
            
            scrollView.contentOffset = CGPointMake(0, bottomCellOffset);
            //到达顶部，这底部的 view 不可滑动
            if (self.canScroll) {
                self.canScroll = NO;
                self.scrollView.scrollEnabled = NO;
                self.containerCell.objectCanScroll = YES;
                [self setSubVcCanScroll:YES];
            }
            _showColor = color;
            [self.navigationController.navigationBar lt_setBackgroundColor:_showColor];
            [self.sectionHeaderView setBackgroundColor: _showColor];
            if (_headerFloating) {
                return;
            }
            [self makeSectionHeaderFloating:YES];
        } else {
            
            //子视图没到顶部
            if (offsetY > NAVBAR_CHANGE_POINT) {
                CGFloat alpha = MIN(1, 1 - ((NAVBAR_CHANGE_POINT + kNavHeight - offsetY) / kNavHeight));
                _showColor = [color colorWithAlphaComponent:alpha];
                [self.navigationController.navigationBar lt_setBackgroundColor:[color colorWithAlphaComponent:alpha]];
                [self.sectionHeaderView setBackgroundColor: [ECTHEMESELECTEDCOLOR colorWithAlphaComponent:alpha]];
                
            } else {
                _showColor = [color colorWithAlphaComponent:0];
                [self.navigationController.navigationBar lt_setBackgroundColor:[color colorWithAlphaComponent:0]];
                [self.sectionHeaderView setBackgroundColor: [color colorWithAlphaComponent:0]];
                
                if (_headerFloating) {
                    [self makeSectionHeaderFloating:NO];
                    
                }
                
            }
            if (!self.canScroll) {
                [self.scrollView setContentOffset:CGPointMake(0, bottomCellOffset) animated:NO];
            }
            
        }
        
        
    }
    
    
}

- (void)makeSectionHeaderFloating:(BOOL)floating
{
    _headerFloating = floating;
    if (floating != self.sectionHeaderView.floating) {
        self.sectionHeaderView.floating = floating;
        
    }
}


-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    [self.navigationController.navigationBar lt_setBackgroundColor:_showColor];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end

